import {
    createRouter,
    createWebHistory,
    RouteRecordRaw,
    createWebHashHistory,
    RouteLocationNormalized,
    NavigationGuardNext
} from 'vue-router'
import Home from '../views/Home/index.vue'

const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        name: 'Home',
        component: Home,
        redirect: '/login',
        children: [
            {
                path: '/workBench',
                name: 'workBench',
                component: () => import('../views/workBench/index.vue')
            },
            {
                path: '/orderList',
                name: 'orderList',
                component: () => import('../views/orderList/index.vue')
            },
            {
                path: '/statisticsDetail',
                name: 'statisticsDetail',
                component: () => import('../views/statisticsDetail/index.vue')
            },
            {
                path: '/classifyList',
                name: 'classifyList',
                component: () => import('../views/classifyList/index.vue')
            },
        ]
    },
    {
        path: '/login',
        name: 'login',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import('../views/login.vue')
    }
]
const isPro: boolean = process.env.NODE_ENV === "prodution";
const router = createRouter({
    //history是路由模式(线上线下)
    history: isPro
        ? createWebHashHistory(process.env.BASE_URL)
        : createWebHistory(process.env.BASE_URL),
    routes,
});
router.beforeEach(
    (
        to: RouteLocationNormalized,
        from: RouteLocationNormalized,
        next: NavigationGuardNext
    ) => {
        const token = localStorage.getItem('token');
        // document.title = to.meta.title;
        // console.log(from);
        if (!token) {
            next();
            return false;
        }
        token ? next() : false;
    }
);


export default router
